package com.tjetc.controller;

import com.github.pagehelper.PageInfo;
import com.tjetc.common.JsonResult;
import com.tjetc.entity.*;
import com.tjetc.service.DishService;
import com.tjetc.service.DishTypeService;
import com.tjetc.service.MerchantDishService;
import com.tjetc.service.MerchantService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;


//商家端增删改查菜的控制器
@RestController
public class MerchantDishController {

    @Autowired
    private DishTypeService dishTypeService;

    @Autowired
    private MerchantDishService merchantDishService;

    //获得菜品清单
    @RequestMapping("merchant/getDish")
    public JsonResult getDish(@RequestParam("pageNum") String pageNum,
                              @RequestParam("pageSize") String pageSize, HttpSession session) {
        Integer merchantId = (Integer) session.getAttribute("id");
        return merchantDishService.getMerchantDish(pageNum, pageSize, merchantId);
    }

    //获得菜品种类
    @RequestMapping("merchant/getDishType")
    public JsonResult getDishType() {
        return dishTypeService.getAll();
    }

    //增加菜品
    @RequestMapping("merchant/addDish")
    public JsonResult addDish(@RequestBody Dish dish, HttpSession session) {
        Integer merchantId = (Integer) session.getAttribute("id");
        return merchantDishService.addDish(dish,merchantId);
    }

    //获得菜品详细信息
    @RequestMapping("merchant/getDishDetail")
    public JsonResult getDishDetail(@RequestParam("dishId") String id) {
        System.out.println(id);
        return merchantDishService.getDishDetailById(id);
    }

    //编辑菜品信息
    @RequestMapping("merchant/updateDish")
    public JsonResult updateDish(@RequestBody Dish dish, HttpSession session) {
        Integer merchantId = (Integer) session.getAttribute("id");
        return merchantDishService.updateDish(dish, merchantId);
    }

    //改变菜品上下架状态
    @RequestMapping("merchant/changeStatus")
    public JsonResult changeStatus(@RequestParam("dishId")String dishId,@RequestParam("status")String status){
        Integer id= Integer.valueOf(dishId);
        Integer statusCode=Integer.valueOf(status);
        return merchantDishService.changeStatus(id,statusCode);
    }

    //获得菜品审核列表
    @RequestMapping("merchant/getExamine")
    public JsonResult getExamin(@RequestParam("pageNum") String pageNum,
                              @RequestParam("pageSize") String pageSize, HttpSession session) {
        Integer merchantId = (Integer) session.getAttribute("id");
        int num= Integer.parseInt(pageNum);
        int size= Integer.parseInt(pageSize);
        return merchantDishService.getMerchantExamine(num,size,merchantId);
    }

    @RequestMapping("merchant/getDishNumber")
    public JsonResult getDishNumber(HttpSession session){
        Integer id= (Integer) session.getAttribute("id");
        return new JsonResult(0,"查询菜品总数成功",merchantDishService.getNumberOfDish(id));
    }

    @RequestMapping("merchant/countDishWith")
    public  JsonResult countDishWith(@RequestBody DishRequest dishRequest, HttpSession session){
        Integer merchantId= (Integer) session.getAttribute("id");
        dishRequest.setMerchantId(merchantId);
        return new JsonResult(0,"查询数量成功",merchantDishService.countDishWith(dishRequest));
    }

    @RequestMapping("merchant/getDishWith")
    public JsonResult getDishWith(@RequestBody DishRequest dishRequest,HttpSession session){
        Integer merchantId= (Integer) session.getAttribute("id");
        dishRequest.setMerchantId(merchantId);
        return merchantDishService.getDishWith(dishRequest);
    }
}
